//设置依赖的配置文件
require.config({
	paths : {
		"jquery" : "jquery",
		"cookie" : "cookie"
	}
})
require(["jquery","cookie"],function($,cookiefn){
	$(".top_right_num").html(cookiefn.getTotal());
	//获取json文件
	var json_date = null;
	$.getJSON('../json/list_goods.json',function(response,status,xhr){
		json_date = response;
		//创建您可能还需要
		for(var i = 0;i < 12;i ++){
			var j = Math.floor(Math.random()*40);
			$date = response[j]
			$(".recommend").append('<div><a href="' + $date.links + '"><img src="../' + $date.paths1 + '"/></a><a href="' + $date.links + '">' + $date.message + '</a><p>￥<span>' + $date.price + '</span></p><button class="add_btn"></button><span class="good_id" style="display:none;">' + $date.goods_id + '</span></div>')
		}
		//给您可能还需要按钮添加事件
		$(".add_btn").click(function(){
			if(cookiefn.getNum($(this).parent().find(".good_id").html(),"goodNum")){
				$(this).parent().find(".good_id").attr("num",parseInt(cookiefn.getNum($(this).parent().find(".good_id").html(),"goodNum")) + 1);
			}else{
				$(this).parent().find(".good_id").attr("num",1);
				//alert(1);
			}
			createCoo($(this).parent().find(".good_id").html(),$(this).parent().find(".good_id").attr("num"))
			location.href = "cart.html";
		})
		//创建购物车表格中的数据
		addCart();
		function addCart(){
			var arr = document.cookie.split("; ");
			for(var i = 0;i < arr.length;i ++){
				var list = arr[i].split("=");
				var obj = eval("(" + decodeURIComponent(list[1]) + ")");
				if(obj){
					if(obj.goodId){
						var totalPrice = obj.goodNum * obj.goodPrice;
						$(".cart_table").append('<tr><td><input type="checkbox" name="checkthis" class="checkthis" /><input class="cart_id" type="text" value="' + obj.goodId + '" hidden="" /></td><td><a href="details.html"><img src="' + obj.goodSrc + '" alt="" /></a></td><td><a href="details.html">' + obj.goodMsg + '</a></td><td class="per_price">￥<span>' + obj.goodPrice + '</span></td><td>-</td><td><span class="sub_num">-</span><input class="num_txt" type="text" value="' + obj.goodNum + '" /><span class="add_num">+</span></td><td class="per_total">￥<span>' + totalPrice + '</span></td><td><a href="#">收藏</a> <a class="per_del" href="#">删除</a></td></tr>')
					}
				}
			}
		}
		//全选复选框操作
		$("#allcheck,#allcheck1").click(function(){
			if($(this).is(':checked')){
				$('#allcheck,#allcheck1,.checkthis').prop('checked',true);
				getTotalMoney();
			}else{
				$('#allcheck,#allcheck1,.checkthis').prop('checked',false);
				$(".total_price").html("0.00");
			}
		});
		//单选框操作
		$(".checkthis").click(function(){
			if($(".checkthis[type='checkbox']:checked").length == $(".checkthis").length){
				$("#allcheck,#allcheck1").prop('checked',true);
				getTotalMoney();
			}else{
				$("#allcheck,#allcheck1").prop('checked',false);
				getTotalMoney();
			}
			
		})
		function getTotalMoney(){
			$sum = 0;
			for(var i = 0;i < $(".checkthis").length;i ++){
				if($(".checkthis").eq(i).is(':checked')){
					//alert($(".checkthis").eq(i).parent().parent().find(".per_total").html());
					$sum += parseInt($(".checkthis").eq(i).parent().parent().find(".per_total span").html());
				}
			}
			if($sum != 0){
				$(".total_price").html($sum);
			}else{
				$(".total_price").html("0.00");
			}
			$(".total_price").html($sum);
		}
		//给购物车里面的加减按钮加事件
		$(".add_num,.sub_num").click(function(){
			$tr = $(this).parent().parent();
			if($(this).attr("class") === "add_num"){
				$goodNum = $tr.find(".num_txt").val() - 0 + 1;
				$per_total = $goodNum * $tr.find(".per_price span").html();
			}else{
				if($tr.find(".num_txt").val() == 1){
					alert("商品数不能小于1");
				}else{
					$goodNum = $tr.find(".num_txt").val() - 1;
					$per_total = $goodNum * $tr.find(".per_price span").html();
				}
			}
			$tr.find(".num_txt").val($goodNum);
			$tr.find(".per_total span").html($per_total);
			createCoo($tr.find(".cart_id").val(),$goodNum);
			getTotalMoney();
		})
		//数目框失焦事件
		$(".num_txt").blur(function(){
			$tr = $(this).parent().parent();
			$tr.find(".per_total span").html($(this).val() * $tr.find(".per_price span").html());
			createCoo($(this).val(),$goodNum);
			getTotalMoney();
		})
		//点击删除按钮
		$(".per_del").click(function(){
			$tr = $(this).parent().parent();
			cookiefn.removeCookie($tr.find(".cart_id").val(),"/");
			$(".top_right_num").html(cookiefn.getTotal());
			$tr.remove();
			getTotalMoney();
			if($(".checkthis[type='checkbox']:checked").length == $(".checkthis").length){
				$("#allcheck,#allcheck1").prop('checked',true);
			}else{
				$("#allcheck,#allcheck1").prop('checked',false);
			}
		})
		//点击删除选中按钮
		$(".del_check").click(function(){
			for(var i = 0;i < $(".checkthis").length;i ++){
				if($(".checkthis").eq(i).is(':checked')){
					$tr = $(".checkthis").eq(i).parent().parent();
					cookiefn.removeCookie($tr.find(".cart_id").val(),"/");
					$(".top_right_num").html(cookiefn.getTotal());
					$tr.remove();
					i --;
				}
			}
			$(".total_price").html("0.00");
			$("#allcheck,#allcheck1").prop('checked',false);
		});
		//点击清空购物车按钮
		$(".empty_cart").click(function(){
			for(var i = 0;i < $(".checkthis").length;i ++){
				$tr = $(".checkthis").eq(i).parent().parent();
				cookiefn.removeCookie($tr.find(".cart_id").val(),"/");
				$(".top_right_num").html(cookiefn.getTotal());
				$tr.remove();
				i --;
			}
			$("#allcheck,#allcheck1").prop('checked',false);
		});
		//创建cookie函数
		function createCoo(id,num){
			//创建并获取cookie
			var cooSrc = "../" + response[id].paths1;
			var cooMsg = response[id].message;
			var cooPrice = response[id].price;
			var cooEva = response[id].num;
			//var cooSale = $obj.parent().find(".sale_message").html();
			var cooNum = num;
			var cooId = id;
			var cooKey = id;
			//alert(cooKey);
			var cookieText = '{"goodSrc":"' + cooSrc + '","goodMsg":"' + cooMsg + '","goodPrice":"' + cooPrice + '","goodEva":"' + cooEva + '","goodNum":"' + cooNum + '","goodId":"' + cooId + '"}';
			cookiefn.createCookie(cooKey,cookieText,7,"/");
			$(".top_right_num").html(cookiefn.getTotal());
		}
	})
})